package com.biubiushop.Socket.TcpAndUdp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author chenjia
 * @date 2019/7/17
 **/
public class UDPServer {

    public static void main(String[] args) throws Exception {
        DatagramSocket datagramSocket = new DatagramSocket(8800);

        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length);

        System.out.println("==============服务器端已启动==============");
        datagramSocket.receive(datagramPacket);

        String info = new String(bytes);

        System.out.println("服务器接收到客户端的信息为:"+info);

        InetAddress address = datagramPacket.getAddress();
        String str = "==========我是服务器=======";
        byte[] bytes1 = str.getBytes();
        DatagramPacket datagramPacket1 = new DatagramPacket(bytes1,bytes1.length,address,datagramPacket.getPort());
        datagramSocket.send(datagramPacket1);
        datagramSocket.close();
    }
}
